package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/editOrDetail")
    public String editOrDetail(Model model ,Long statementId ){
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {

            return prefix + "edit";
        } else {
            return prefix + "detail";
        }
    }
    @RequiresPermissions("business:statementItem:query")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(StatementItemQuery qo){
        TablePageInfo< StatementItem > query = statementItemService.query(qo);
        System.out.println(query);
        return statementItemService.query(qo);
    }

    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List< StatementItem > list){
        statementItemService.saveItems(list);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:pay")
    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId){
        statementItemService.pay(statementId);
        return AjaxResult.success();
    }
    @RequiresPermissions("business:statementItem:exportStatement")
    @RequestMapping("/exportStatement")
    @ResponseBody
    public void exportStatement(HttpServletResponse response, Long statementId) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=statemetnData.xlsx");
        Workbook workbook = statementItemService.exportStatement(statementId);
        workbook.write(response.getOutputStream());
    }
}
